import {
  predictionsReq
} from '@/requests/authorRelationReq.js';

import echarts from 'echarts';

function randomHexColor() {	//随机生成十六进制颜色
	return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).substr(-6);
}

let asyncData = new Object();

function setPredictionMap(name, id, router = null) {
  predictionsReq(id)
  .then(function (response) {
    asyncData = new Object();
    asyncData = response.data.data;
    init(name, id, router);
  })
  .catch(function (error) {
    console.log(error);
  });
}

const init = (authorName, authorId, router) => {
  const degrees = [...Array(360).keys()]

  // 需要添加name和data
  const seriesSet = {
    type: 'scatter',
    label: {
      normal: {
        show: true,//显示
        formatter: function(params){
          return params.value[6];
        },
        position: 'bottom'
      }
    },
    coordinateSystem: 'polar',
    symbolSize: function (val) {
      return val[2] * 2;
    },
    itemStyle: {
      emphasis: {
        shadowBlur: 10,
        shadowColor: '#fff'
      }
    },
  };

  var days = new Array(120).fill('');
  days[0] = 'close';

  const data_list = [[{
    value: [0, 0, 30, authorId, authorName],
    label: {
      color: '#ff8d8d',
      position: 'inside'
    },
  }]];
  data_list.push([]);

  const predictions = asyncData.predictions;
  predictions.map((prediction, i) => {
    const authors = prediction.authors;
    // 基准
    const base = 60 * 360;
    // 偏移量
    const offset = 360 * 60 / predictions.length;
    const x = parseInt(((base + i * offset) / 360).toFixed());
    const y = parseInt(((base + offset) % 360).toFixed()) - Math.round(Math.random() * offset % 360);
    data_list[1].push({
      value: [x, y,
        9 + Math.round(Math.random() * 4),
        prediction.distance, prediction.endId, prediction.directions, prediction.endName], 
      itemStyle: {
        normal: {
          color: randomHexColor()
        }
      }
    }); 
  });

  const name_list = ['Origin Author', 'Predicted Cooperated Authors'];
  const series = data_list.map((value, index) => {
    const seriesItem = Object.assign({}, seriesSet);
    seriesItem.name = name_list[index];
    seriesItem.data = value;
    return seriesItem;
  });

  const option = {
      title: {
        text: 'Cooperation Predictions',
      },
      legend: {
        left: 'right',
    },
      polar: {radius: '97%'},
      tooltip: {
        formatter: function (params) {
          return `Author Id: ${params.data.value[4]}<br/>
            Name: ${params.data.value[6]}<br/>
            Social distance: ${params.data.value[3]}<br/>
            Possible Cooperate Directions: <br/>
            · ${params.data.value[5].slice(0, 5).join('<br/>· ')}`;
        }
      },
      angleAxis: {
        type: 'category',
        data: degrees,
        boundaryGap: false,
        axisLine: {
          show: false
        },
        axisLabel: {
          show: false
        },
        axisTick:{       //y轴刻度线
          "show":false
        },
      },
      radiusAxis: {
        type: 'category',
        data: days,
        // boundaryGap: false,
        axisLine: {
          show: false
        },
        splitLine: {
          show: true
        },
        markLine: {
          show: false
        },
        axisLabel: {
          show: false,
        },
        axisTick:{       //y轴刻度线
          "show":false
        },
      },
      series
  };


  var myChart = echarts.init(document.getElementById('predictions-map'));

  myChart.setOption(option)

  myChart.on('click', function (params) {
    // 在用户点击后控制台打印数据的名称
    const id = params.data.value[4];
    router.push({
      name: 'Author',
      params: {
        id
      }
    })
  });
  return myChart;
}

export default setPredictionMap;